/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "grf_TDC.h"
using namespace grf;

// TDC ////////////////////////////////////////////////////////////////////////
/*************
//
void TDC::SelectClipLP(TRegion &V,ERegionMode RgnMode)
{
  //Except("SelectClipLP");

  int RegionSize = ::GetRegionData(V,0,NULL); 
  if (RegionSize == 0) {
    ::ExtSelectClipRgn(Handle,V,RgnMode);
    return;
  }

  ::RGNDATA *PRegion = (::RGNDATA *)new char[RegionSize];
  if (!PRegion) {
    delete PRegion;
    return;
  }

  ::GetRegionData(V,RegionSize,PRegion);
  ::RECT *R = (::RECT*)&PRegion->Buffer;

  for (DWORD N = 0; N < PRegion->rdh.nCount; N++)
  {

	  int W = R[N].right  - R[N].left;
	  int H = R[N].bottom - R[N].top;

    R[N].left   = MMtoPT(R[N].left);
    R[N].top    = MMtoPT(R[N].top);
    R[N].right  = R[N].left + MMtoPT(W);
    R[N].bottom = R[N].top  + MMtoPT(H);


    //R[N].right  = K(R[N].right);
    //R[N].bottom = K(R[N].bottom);


    LPtoDP(R[N]);
    //R[N] = R[N];
  }

  ::HRGN HRegion = ExtCreateRegion(NULL,RegionSize,PRegion);
  ::ExtSelectClipRgn(Handle,HRegion,RgnMode);

  ::DeleteObject(HRegion);
  delete PRegion;
}
*****************/

//
void TDC::DrawHorzDotLine(int X,int Y,int W)
{
  TPen Pen(TColor(123,123,123)); Select(Pen);
  int x=/*MMtoPT*/(X),y=/*MMtoPT*/(Y),w=/*MMtoPT*/(W);

  int P = x + w; for (; x <= P; x += 2)
    { MoveTo(x,y); LineTo(x+1,y); }
}

//
void TDC::DrawVertDotLine(int X,int Y,int W)
{
  TPen Pen(TColor(123,123,123)); Select(Pen);
  int x=/*MMtoPT*/(X),y=/*MMtoPT*/(Y),w=/*MMtoPT*/(W);

  int P = y + w; for (; y <= P; y += 2)
    { MoveTo(x,y); LineTo(x,y+1); }
}

//
bool TDC::Scroll(int InDx,int InDy,TRect InRScroll,TRect InRClip,TRect &OutRUpdate)
{
/**********
  int MMp = 10;
  int PTp = static_cast<int>( ( (double) (MMp << 10) / 3000 ) ) >> 10;
      MMp = static_cast<int>( ( (double) (PTp << 10) * 3000 ) ) >> 10;

  int MMm = -10;
  int PTm = static_cast<int>( ( (double) (MMm << 10) / 3000 ) ) >> 10;
      MMm = static_cast<int>( ( (double) (PTm << 10) * 3000 ) ) >> 10;
************/

/**********
int Correction[10]; for (int V = 100,N = 0; N < 10; N++) {

}

scale=2;
for (int N = 0; N <= 20; N++) {
  int MM  = 100 + N; 
  int PT  = MulDiv(MM,10000,static_cast<int>(scale*10000));
  int MMc = MulDiv(PT,static_cast<int>(scale*10000),10000); PT = 

  
  //div_t DT = div(PT,2); //PT += DT.rem;
  int MMx = MulDiv(PT,static_cast<int>(scale*10000),10000); //MM -= DT.rem;
               
  MMx=MMx;
}          
***********/      

//WorldTransform

  //int PT = MMtoPT_Scroll(10); 
  //int MM = PTtoMM_Scroll(PT << 10) >> 10; 
  //YY = YY >> 10;

/*****************
  int kkk = 12;

  int dx = InDx;
  int dy = abs(InDy);

  int top = InRScroll.Top;
  int height = InRScroll.Height;


  InDx = InDx << kkk; 
  InDx = MMtoPT_Scroll(InDx); 
  InDx = InDx >> kkk;
  
  InDy = InDy << kkk;
  InDy = MMtoPT_Scroll(InDy); 
  InDy = InDy >> kkk;
  
  InRScroll.Left   = InRScroll.Left << kkk;
  InRScroll.Top    = InRScroll.Top << kkk;
  InRScroll.Width  = InRScroll.Width << kkk;
  InRScroll.Height = InRScroll.Height << kkk;
  InRScroll.MMtoPT_Scroll(scale);
  InRScroll.Left   = InRScroll.Left >> kkk;
  InRScroll.Top    = InRScroll.Top >> kkk;
  InRScroll.Width  = InRScroll.Width >> kkk;
  InRScroll.Height = InRScroll.Height >> kkk;
  
  InRClip = InRScroll;
  

  //InRScroll.MMtoPT(scale); InRClip.MMtoPT(scale);
  ::RECT RScroll = InRScroll,RClip = InRClip,RUpdate;
  //K(RScroll);
  //K(RClip);
  //TRegion RgnUpdate;

//scale=scale;
//int Fp=MMtoPT(200 << 10);
//    Fp div 1024
//int Fm=PTtoMM(Fp) >> 10;


  bool Ret = ::ScrollDC(
    Handle,InDx,InDy,&RScroll,&RClip,NULL,//RgnUpdate
    &RUpdate
  ) != 0;
  
  OutRUpdate = RUpdate; 

  int outTop = OutRUpdate.Top;
  int outHeight = OutRUpdate.Height;

  OutRUpdate.Left = OutRUpdate.Left << kkk;
  OutRUpdate.Top = OutRUpdate.Top << kkk;
  OutRUpdate.Width = OutRUpdate.Width << kkk;
  OutRUpdate.Height = OutRUpdate.Height << kkk;

  OutRUpdate.PTtoMM_Scroll(scale); 

  OutRUpdate.Left = OutRUpdate.Left >> kkk;
  OutRUpdate.Top = OutRUpdate.Top >> kkk;
  OutRUpdate.Width = OutRUpdate.Width >> kkk;
  OutRUpdate.Height = OutRUpdate.Height >> kkk;

  return Ret;
************************/


  //InDx = MMtoPT(InDx); 
  //InDy = MMtoPT(InDy); 
  //InRScroll.MMtoPT(scale);
  //InRClip.MMtoPT(scale);
  
  ::RECT RScroll = InRScroll,RClip = InRClip,RUpdate;

  bool Ret = ::ScrollDC(Handle,InDx,InDy,&RScroll,&RClip,NULL,&RUpdate) != 0;
  OutRUpdate = RUpdate; 
  //OutRUpdate.PTtoMM(scale); 

  return Ret;
}
